﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace market
{
    public partial class Login : System.Web.UI.Page
    {
        CommonClass cobj = new CommonClass();
        DBClass dbobj = new DBClass();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.lbCode.Text = cobj.RandomNum(4);
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (this.txtName.Text.Trim() == "" || this.txtPsd.Text.Trim() == "")
            {
                Response.Write(cobj.MessageBox("登录名和密码不能为空！"));
                this.lbCode.Text = cobj.RandomNum(4);
            }
            else
            {
                //判断用户输入的验证码是否正确
                if (txtCode.Text.ToLower().Trim() == lbCode.Text.ToLower().Trim())
                {
                    string strSql = "select * from Admin where AdminName='" + this.txtName.Text.Trim() + "' and Password='" + this.txtPsd.Text.Trim() + "'";
                    DataTable dsTable = dbobj.GetDataSetStr(strSql, "tbAdmin");
                    //判断用户是否存在
                    if (dsTable.Rows.Count > 0)
                    {
                        Session["AID"] = Convert.ToInt32(dsTable.Rows[0][0].ToString());
                        Session["AName"] = dsTable.Rows[0][1].ToString();
                        Response.Redirect("main.aspx");
                    }
                    else
                    {
                        Response.Write(cobj.MessageBox("您输入的用户名或密码错误，请重新输入！"));
                        this.lbCode.Text = cobj.RandomNum(4);
                    }
                }
                else
                {
                    Response.Write(cobj.MessageBox("验证码输入有误，请重新输入！"));
                    this.lbCode.Text = cobj.RandomNum(4);
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Write("<script>window.close();location='javascript:history.go(-1)';</script>");
        }
    }
}